//
//  SearchAfterViewController.m
//  UGoods
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 蓝鸥科技.李贺. All rights reserved.
//

#import "SearchAfterViewController.h"
#import "LHAFNetworkTool.h"
#import "FindModel.h"
#import "BaseCollectionView.h"
#import "GoodsCollectionViewCell.h"
#import "BuyDetailViewController.h"
#import "ConstantsDefine.h"

@interface SearchAfterViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) BaseCollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataArray;

@end

@implementation SearchAfterViewController

- (void)dealloc
{
    [_searchText release];
    [_collectionView release];
    [_dataArray release];
    [super dealloc];
}

- (void)handleData {
    NSString *str = [NSString stringWithFormat:@"http://api.ujipin.com/v2/app/ios/phone/v3/search/?channel=appstore&idfa=1EF25F55-6367-4567-B0D5-82DF4F1557CD&user_id=470825&plantform=iphone&version=320&keywords=%@&page_size=20&ss=0&production=0&price_min=0&price_max=0&page=1", self.searchText];
    NSLog(@"%@", str);
    [LHAFNetworkTool getUrl:str body:nil response:LHJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
            // 判断是否搜到
            if (![resposeObject[@"message"] isEqualToString:@"no keywords"] && ![resposeObject[@"data"][@"count"] isEqualToNumber:@0]) {
                NSArray *array = resposeObject[@"data"][@"goods_list"];
                self.dataArray = [FindModel modelHandleWithArray:array];
                
                [self.collectionView reloadData];
                self.navigationItem.title = self.searchText;
            } else {
                UILabel *alabel = [[UILabel alloc] initWithFrame:self.view.bounds];
                alabel.text = @"無";
                alabel.textColor = [UIColor whiteColor];
                alabel.backgroundColor = [UIColor blackColor];
//                alabel.font = [UIFont fontWithName:@"Zapfino" size:300];
                alabel.font = [UIFont boldSystemFontOfSize:300];
                alabel.textAlignment = NSTextAlignmentCenter;
                [self.view addSubview:alabel];
                [alabel release];
            }
           
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            
        }];
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 30 * WIDTH) / 2, 250 * HEIGHT);
    layout.minimumInteritemSpacing = 10 * WIDTH;
    layout.minimumLineSpacing = 10 * HEIGHT;
    layout.sectionInset = UIEdgeInsetsMake(0, 10 * WIDTH, 10 * HEIGHT, 10 * WIDTH);
    
    self.collectionView = [[BaseCollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    //    _collectionView.backgroundColor = [UIColor clearColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    [_collectionView release];
    [layout release];
    [_collectionView registerClass:[GoodsCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GoodsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    FindModel *model = self.dataArray[indexPath.item];
    cell.model = model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    FindModel *model = self.dataArray[indexPath.item];
    BuyDetailViewController *buy = [[BuyDetailViewController alloc] init];
    buy.model = model;
    [self.navigationController pushViewController:buy animated:YES];
    [buy release];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self handleData];
    [self createCollectionView];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
